package com.example.kotlinlearndemo

import android.view.View
import androidx.databinding.Observable
import androidx.databinding.ObservableField
import androidx.lifecycle.*
import kotlinx.coroutines.launch

class MyViewModel : ViewModel() {

    init {
        viewModelScope.launch {
        }
    }

    var text: String = "点击"
    var text2: String = "点击2"
    var text3: String = "点击3"
    var text4: String = "点击4"

    val checked = ObservableField<Boolean>(true)
    fun reverse(view: View) {
        checked.get()?.let {
            checked.set(!it)
        }
    }

    val observerValue = ObservableField<String>("初始值2")
    private val observerLiveData = MutableLiveData<String>()

    fun observe(owner: LifecycleOwner) {
        observerLiveData.observe(owner) { newValue ->
            println("值发生变化了 $newValue")
            observerValue.set(newValue)
        }

        checked.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
            override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                println("propertyId: $propertyId")
                if (sender is ObservableField<*>) {
                    println("sender: ${sender.get()}")
                }
            }
        })
    }

    fun changeValue(view: View) {
        println("changeValue")
        observerLiveData.value = "变化值" + System.currentTimeMillis()
    }

    fun test(view: View) {
        println("test")
    }

    fun test2() {
        println("test2")
    }

    override fun onCleared() {
        super.onCleared()
    }

    val expandViewClick2 = View.OnClickListener {

    }

    fun click1(view: View) {
        println("click1")
    }

    fun click2(view: View) {
        println("click2")
    }
}